package com.axiang.module_main;

import androidx.annotation.NonNull;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.view.MenuItem;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axiang.lib_common.mvvm.BaseActivity;
import com.ren.lib_path.router.RouterFragmentPath;
import com.axiang.lib_common.utils.RxToast;

public class MainActivity extends BaseActivity {

    private long mBackPressed;
    private static final int TIME_INTERVAL = 2000;

    private ViewPager viewPager;
    private BottomNavigationView navigation;

    @Override
    public int onBindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public boolean enableToolbar() {
        return false;
    }

    @Override
    public void initView() {
        viewPager = findViewById(R.id.viewPager);
        navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int i = menuItem.getItemId();

                if (i == R.id.navigation_first) {
                    viewPager.setCurrentItem(0);
                } else if (i == R.id.navigation_second) {
                    viewPager.setCurrentItem(1);
                } else if (i == R.id.navigation_third) {
                    viewPager.setCurrentItem(2);
                } else if (i == R.id.navigation_fouth) {
                    viewPager.setCurrentItem(3);
                }
                return false;
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                navigation.getMenu().getItem(i).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));

    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        Fragment homeFragment = (Fragment) ARouter.getInstance().build(RouterFragmentPath.Home.PAGER_HOME).navigation();
        Fragment workFragment = (Fragment) ARouter.getInstance().build(RouterFragmentPath.Work.PAGER_WORK).navigation();
        Fragment signinFragment = (Fragment) ARouter.getInstance().build(RouterFragmentPath.Home.PAGER_HOME).navigation();
        Fragment meFragment = (Fragment) ARouter.getInstance().build(RouterFragmentPath.User.PAGER_ME).navigation();
        private Fragment[] mFragments = new Fragment[]{homeFragment,workFragment,signinFragment,meFragment};

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return mFragments[i];
        }

        @Override
        public int getCount() {
            return mFragments.length;
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void onBackPressed() {
        if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) {
            super.onBackPressed();
            return;
        } else {
            RxToast.showToastShort("再次点击返回键退出");
        }
        mBackPressed = System.currentTimeMillis();
    }
}
